<?php
/**
* This is a representation of an user in DB
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
* @package User
*/
class User
{
    private $dbinterface;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->dbinterface = new DBInterface();
    }

    /**
    * Insert a new user in th DB
    * @param Array $data An associative array with the user data
    * @return Bool Indicates if the insertion was succesful
    */
    function create($data)
    {
        $data['usu_password'] = md5($data['usu_password']);
        return $this->dbinterface->exec_insert($data,'user_wp');
    }

    /**
    * Modify a single field of one user
    * @param String $fld_name Field name
    * @param String $fld_value Field value
    * @param Integer $id Id of the user to be modified
    */
    function modify($fld_name,$fld_value,$id)
    {
        $data = array();
        $data[$fld_name] = $fld_value;
        return $this->dbinterface->exec_update($data,'user_wp','usu_id='.$id);
    }

    /**
    * Delete a user
    * @param Integer $id Id of the user to be deleted
    */
    function remove($id)
    {
        $this->dbinterface->exec_delete('user_wp','usu_id='.$id);
    }

    /**
    * Retrieve the information of one user
    * @param Integer $id Id of the user
    * @return Array Array with the user data
    */
    function getData($id)
    {
        $fields = array('usu_id','usu_username','usu_first_name','usu_last_name','usu_profile','usu_email','usu_description');
        $result_set = $this->dbinterface->exec_select($fields,'user_wp','usu_id='.$id);
        return $result_set[0];
    }

    /**
    * Retrieve a list of users accord of search criteria
    * @param String $name A part or complete value to the search
    * @param String $search_type The type of search to the criteria
    * @return Array Bidimensional array with the users data
    */
    function getList($name='',$search_type='')
    {
        $fields = array('usu_id','usu_first_name','usu_last_name','usu_profile');
        $criteria = "";
        if($search_type=='first_name')
            $criteria .= "usu_first_name ilike '%".$name."%'";
        elseif($search_type=='last_name')
            $criteria .= "usu_last_name ilike '%".$name."%'";
        elseif($search_type=='both')
            $criteria .= "(usu_first_name || ' ' || usu_last_name) ilike '%".$name."%'";
        $order = "order by usu_id";
        return $this->dbinterface->exec_select($fields,'user_wp',$criteria,$order);
    }

    /**
    * Obtains a name list of users
    * @param String $name A part or complete value to the search
    * @param String $search_type The type of search to the criteria
    * @return Array The names array
    */
    function getNames($name, $search_type)
    {
        if($search_type=='first_name')
        {
            $criteria = "usu_first_name ilike '%".$name."%'";
            $fields = array('usu_first_name as name');
        }
        elseif($search_type=='last_name')
        {
            $criteria = "usu_last_name ilike '%".$name."%'";
            $fields = array('usu_last_name as name');
        }
        elseif($search_type=='both')
        {
            $criteria = "(usu_first_name || ' ' || usu_last_name) ilike '%".$name."%'";
            $fields = array("(usu_first_name || ' ' || usu_last_name) as name");
        }

        return $this->dbinterface->exec_select($fields,'user_wp',$criteria,'',true);
    }

    /**
    * Validate if exist a user with the same input username
    * @param String $name Input username
    * @return Bool Indicates if exists a user
    */
    function exist($name)
    {
        $users = $this->dbinterface->exec_select(array('usu_id'),'user_wp',"usu_username='".$name."'");

        if($users==0)
            return false;
        else
            return true;
    }

    /**
    * Validate if a user has a game associated
    * @param Integer $id Id of the user
    * @return Bool Indicates if a user has games
    */
    function hasGames($id)
    {
        $fields = array('count(gme_id) as games','gme_referee_1');
        $table = array('user_wp','game');
        $criteria = "usu_id=gme_referee_1 and usu_profile=3 and usu_id=".$id;
        $group = "group by gme_referee_1";
        $games = $this->dbinterface->exec_select($fields,$table,$criteria,$group);
        if($games!=0)
            return true;
        $fields[1] = 'gme_referee_2';
        $criteria = "usu_id=gme_referee_2 and usu_profile=3 and usu_id=".$id;
        $group = "group by gme_referee_2";
        $games = $this->dbinterface->exec_select($fields,$table,$criteria,$group);
        if($games!=0)
            return true;
        else
            return false;
    }
}
?>